//
//  NMineViewController.m
//  nncloud
//
//  Created by kabu on 2018/9/15.
//  Copyright © 2018年 youedata. All rights reserved.
//

#import "NMineViewController.h"
#import "NMineCell.h"
#import "NLoginViewController.h"
#import "NRoleSelectedViewController.h"
#import "NMyFeedbackViewController.h"
#import "NProfileModifyViewController.h"
#import "NAuthViewController.h"
#import "NFillDataViewController.h"
#import "NPersonalFillDataVC.h"//个人认证
#import "NAuthMessageViewController.h"
//#import "NAuthTipViewController.h"
#import "NAuthFirstStepViewController.h"
#import "NMessageViewController.h"


@interface NMineViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *picArray;
@property (nonatomic,strong)NSArray *titles;
///审核状态
@property (nonatomic,copy)NSString *authType;

@property (nonatomic,copy)NSString *headUrl;


@end

#define cellIdentifier @"mineid"
#define SectionHeaderHeight  15.f

@implementation NMineViewController

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64-49) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NMineCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    ///控制红点显示
    if ([NUserModel shareInstance].isLogin) {
        if ([[NUserModel shareInstance].badgeCount integerValue]>0) {
            [self.navigationItem.rightBarButtonItem pp_addDotWithColor:[UIColor redColor]];
        }
    }else{
         [self.navigationItem.rightBarButtonItem pp_addDotWithColor:nil];
    }
    
    
    if ([NUserModel shareInstance].isLogin) {
        self.picArray = @[@"",@"bind",@"feedback",@"collcet",@"transfer"];
    }else{
        //未登录
        self.picArray = @[@"headicon",@"bind",@"feedback",@"collcet",@"transfer"];
    }
    self.titles = @[[NUserModel shareInstance].isLogin?[NUserModel shareInstance].userInfoName:@"请登录",@"认证",@"我的反馈",@"我的收藏",@"切换用户角色"];
    
    if ([NUserModel shareInstance].isLogin) {
        [self loadUserData];
    }
    
    [self.tableView reloadData];
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"我的" textColor:kNavigationTitleColor fontSize:18 itemType:center];
     [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 40, 40) title:@"" Image:@"消息中心" selectedIMG:@"消息中心" tartget:self action:@selector(messageAction)];
    
   
    self.navigationItem.leftBarButtonItem = nil;
    [self.view addSubview:self.tableView];
    
//    self.picArray = @[[NUserModel shareInstance].isLogin?@"head":@"headicon",@"mine1",@"mine2",@"mine3",@"mine4"];
//    self.titles = @[[NUserModel shareInstance].isLogin?[NUserModel shareInstance].userInfoName:@"请登录",@"企业绑定",@"我的反馈",@"我的收藏",@"切换用户角色"];
//    if ([NUserModel shareInstance].isLogin) {
//         [self loadUserData];
//    }
   
    // Do any additional setup after loading the view from its nib.
}

///消息中心
- (void)messageAction {
    YYLog(@"跳往消息中心");
    NMessageViewController *msg = [NMessageViewController new];
    msg.userType = [NUserModel shareInstance].userInfoType;
    msg.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:msg animated:YES];
    
}

///
- (void)loadUserData{
    
    WeakStament(ws);
    NSDictionary *dic = @{@"userInfoId":[NUserModel shareInstance].userInfoId?[NUserModel shareInstance].userInfoId:@""};
    [[NNRCNetRequest manager]getWithUrl:kUserInfoUrl params:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"code"] integerValue]==0) {
            self.authType = result[@"page"][@"userInfoCertificationStatus"];
            self.headUrl = result[@"page"][@"userInfoUrl"];
            [NUserModel shareInstance].userInfoCertificationStatus = self.authType;
            YYLog(@"审核状态:%@",self.authType);
            [ws.tableView reloadData];
        }
    } failure:^(id failure) {
        YYLog(@"%@",failure);
    }];
}
///调用用户切换页面

-(void)callTransferVC{
    
    NRoleSelectedViewController *role = [[NRoleSelectedViewController alloc] initWithNibName:@"NRoleSelectedViewController" bundle:nil];
    //    NLoginViewController *login = [[NLoginViewController alloc]initWithNibName:@"NLoginViewController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:role];
    //解决present延迟弹出
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:nav animated:YES completion:^{
            
        }];
    });
}

#pragma mark ===== 去除警告Category is implementing a method which will also be implemented by its primary class
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0:
        {
            
            if ([NUserModel shareInstance].isLogin) {
                
                NProfileModifyViewController *modify = [NProfileModifyViewController new];
                modify.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:modify animated:YES];

            }else{
                 [self callLoginVC];
            }
        }
            break;
        case 1:
        {
            
            if (![NUserModel shareInstance].isLogin) {
                 [self callLoginVC];
                return;
            }
            ///这里跳转要根据认证状态跳转不同的页面
            
            ///0等待审核     1已认证    2未通过   3未认证
            NSInteger type = [self.authType integerValue];
            
            if (type==3) {
                //未认证  然后进行认证
                NAuthFirstStepViewController *firstAuth = [NAuthFirstStepViewController new];
                firstAuth.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:firstAuth animated:YES];
                
            }else {
                // 认证相关信息查询   审核中 未通过  已通过
                NAuthMessageViewController *msg = [NAuthMessageViewController new];
                msg.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:msg animated:YES];
            }
        }
            break;
        case 2:
        {
            //我的反馈
            if ([NUserModel shareInstance].isLogin) {
                NMyFeedbackViewController *feedback = [NMyFeedbackViewController new];
                feedback.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:feedback animated:YES];
            }else{
                [self callLoginVC];
            }
        }
            break;
        case 3:
        {
            //我的收藏
            if ([NUserModel shareInstance].isLogin) {
                NMyFeedbackViewController *feedback = [NMyFeedbackViewController new];
                feedback.hidesBottomBarWhenPushed = YES;
                feedback.flag = @"like";
                [self.navigationController pushViewController:feedback animated:YES];
            }else{
                [self callLoginVC];
            }
        }
            break;
        case 4:
        {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"确认重新选择用户角色? \n 已登录帐号将退出"
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"确认",nil];
            [alert show];
            
        }
            break;
            
        default:
            break;
    }
    
    
}

#pragma mark - delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        
        YYLog(@"点击了一下");
        
    }else{
        
        [self callTransferVC];
        
    }
}


#pragma clang diagnostic pop

#endif

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        
        return 90.f;
        
    }else{
        return 55.f;
    }
}

#pragma mark - datasource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  self.titles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NMineCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    [cell configWithPicName:self.picArray[indexPath.section] title:self.titles[indexPath.section] withIndexPath:indexPath];
    
    if (indexPath.section==0) {
        
        cell.setLabel.hidden = YES;
        
        if ([NUserModel shareInstance].isLogin) {
             [cell.LogoImage sd_setImageWithURL:[NSURL URLWithString:self.headUrl] placeholderImage:WDImgName(@"headicon")];
        }else{
            cell.LogoImage.image = WDImgName(@"headicon");
        }
       
    }
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SectionHeaderHeight;//如果设置section的header高度，不设置footer高度，footer默认等于header高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
    view.backgroundColor = kBackgroundColor;
    return view;
}
@end
